package com.red.star.wechat.data.repository;

import com.red.star.wechat.data.core.constant.MongoConstant;
import com.red.star.wechat.data.entity.ChatList;
import com.red.star.wechat.data.entity.ChatUser;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/2/6.
 * 聊天列表数据库访问类
 */
@Repository
public class ChatListRepository {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 查询小程序用户的聊天列表
     *
     * @param fansId 小程序粉丝ID
     * @return 聊天列表
     */
    public List<ChatUser> findAllChatUserByFansId(Integer fansId) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        ChatList chatList = mongoTemplate.findOne(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST);
        if (chatList != null) {
            return chatList.getChatUsers();
        }
        return null;
    }

    /**
     * 查询E端管理员的聊天列表
     *
     * @param adminId E端管理员ID
     * @return 聊天列表
     */
    public List<ChatUser> findAllChatUserByAdminId(Integer adminId) {
        Query query = Query.query(Criteria.where("admin_id").is(adminId));
        ChatList chatList = mongoTemplate.findOne(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST);
        if (chatList != null) {
            return chatList.getChatUsers();
        }
        return null;
    }

    /**
     * 添加聊天对象到小程序粉丝聊天列表
     *
     * @param fansId   小程序粉丝ID
     * @param chatUser 聊天对象
     * @return 影响行数
     */
    public int addUserToFansChatList(Integer fansId, ChatUser chatUser) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        Update update = Update.update("update_time", new Date()).push("chat_users", chatUser);
        int n = mongoTemplate.upsert(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }

    /**
     * 从小程序粉丝聊天列表中删除某个聊天对象
     *
     * @param fansId   小程序粉丝ID
     * @param chatUser 聊天对象
     * @return 删除行数
     */
    public int deleteUserFromFansChatList(Integer fansId, ChatUser chatUser) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        Update update = Update.update("update_time", new Date()).pull("chat_users", chatUser);
        int n = mongoTemplate.updateFirst(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }

    /**
     * 清除小程序粉丝的聊天列表
     *
     * @param fansId 小程序粉丝ID
     * @return 删除行数
     */
    public int clearFansChatList(Integer fansId) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        return mongoTemplate.remove(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
    }

    /**
     * 添加聊天对象到E端管理员聊天列表
     *
     * @param adminId  E端管理员ID
     * @param chatUser 聊天对象
     * @return 影响行数
     */
    public int addUserToAdminChatList(Integer adminId, ChatUser chatUser) {
        Query query = Query.query(Criteria.where("admin_id").is(adminId));
        Update update = Update.update("update_time", new Date()).push("chat_users", chatUser);
        int n = mongoTemplate.upsert(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }

    /**
     * 从E端管理员聊天列表删除聊天对象
     *
     * @param adminId  E端管理员ID
     * @param chatUser 聊天对象
     * @return 删除行数
     */
    public int deleteUserFromAdminChatList(Integer adminId, ChatUser chatUser) {
        Query query = Query.query(Criteria.where("admin_id").is(adminId));
        Update update = Update.update("update_time", new Date()).pull("chat_users", chatUser);
        int n = mongoTemplate.updateFirst(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }

    /**
     * 清除E端管理员聊天列表
     *
     * @param adminId E端管理员ID
     * @return 删除行数
     */
    public int clearAdminChatList(Integer adminId) {
        Query query = Query.query(Criteria.where("admin_id").is(adminId));
        return mongoTemplate.remove(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
    }

    /**
     * 查询三十分钟内活跃的用户
     *
     * @return
     */
    public List<ChatList> findActiveUserInThirtyMinute() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -30);
        Query query = Query.query(Criteria.where("update_time").gt(calendar.getTime()));
        return mongoTemplate.find(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST);
    }

    public int clearHomeUserList(Integer fansId, String mallCode) {
        ChatUser chatUser = new ChatUser();
        chatUser.setMallCode(mallCode);
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        Update update = Update.update("update_time", new Date()).pull("chat_users", chatUser);
        int n = mongoTemplate.updateMulti(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }
}
